#!/bin/bash

verbose=$1
host=linux
if [ `uname` = "Darwin" ] || [ `uname | egrep -i darwin` ]; then
    host=macosx
fi

function exec() 
{
    echo $1
    eval $1 $verbose
    if [ $? -ne 0 ]
        then exit -1
    fi
    echo ""
}

function build() 
{
    # enter project
    echo "build $1"
    cd $1

    # build
    exec "xmake f -c"
    exec "xmake "
    exec "xmake p"

    # leave project
    cd -
}

function build_for_iphoenos() 
{
    # enter project
    echo "build $1"
    cd $1

    # package all archs for iphoneos
    exec "xmake m package -p iphoneos" 

    # leave project
    cd -
}

# build for c
build "./tests/console_c" 
build "./tests/static_library_c" 
build "./tests/shared_library_c" 

# build for c++
build "./tests/console_c++"
build "./tests/static_library_c++" 
build "./tests/shared_library_c++" 

# merge object 
build "./tests/merge_object"

# merge static library
build "./tests/merge_static_library"

# build for macosx
if [ $host = "macosx" ]; then

    # build for objc/c++
    build "./tests/console_objc" 
    build "./tests/console_objc++" 
    build_for_iphoenos "./tests/console_objc" 
    build_for_iphoenos "./tests/console_objc++" 

    # build for swift
    build "./tests/console_swift" 
    build_for_iphoenos "./tests/console_swift" 

    # build for go
    build "./tests/console_go" 
    build "./tests/static_library_go" 

    # build for dlang
    build "./tests/console_dlang" 
    build "./tests/static_library_dlang" 

    # build for rust
    build "./tests/console_rust" 
    build "./tests/static_library_rust" 
fi

